﻿//
//  ZHDataService.m
//  WDFans
//
//  Created by Zhihong Yuan on 2/7/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ZHWebService.h"
#import "BlockDownloader.h"
#import "ZHDataService.h"

@interface ZHWebService()
@end

@implementation ZHWebService

#pragma -
#pragma singleton design
+ (id)hiddenAlloc
{
    return [super alloc];
}

+ (id)alloc
{
    NSLog(@"Use +sharedInstance instead of +alloc.");
    return nil;
}

+ (id)new
{
    return [self alloc];
}

- (id)copyWithZone:(NSZone *)zone
{
    NSLog(@"Users: attempt to -copy may be a bug(singleton)");
    return self;
}

- (id)mutableCopyWitZone:(NSZone *)zone
{
    return [self copyWithZone:zone];
}

#pragma mark - Url encoding
//nsstring url encode
-(NSString *)urlEncodeValue:(NSString *)str
{
//	NSString *result = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)str, NULL, CFSTR(" :/?#[]@!$&’()*+,;="), kCFStringEncodingUTF8);
    //return [result autorelease];
    //[NSString  stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
	return [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}

#pragma -
#pragma data services methods

+ (ZHWebService *)sharedService
{
    static dispatch_once_t once;    
    static ZHWebService *dataService = nil;
    
    dispatch_once(&once, ^{
        dataService = [[ZHWebService hiddenAlloc] init];
    });
    return dataService;
}

- (void)getPersonalThemes
{
    return;
}

- (void)getRecentNewsWithPage:(NSInteger)page 
                   onCompletion:(CompletionBlock)comBlock 
                        onError:(ErrorBlock)errBlock
{
    BlockDownloader *recentNews = [[BlockDownloader alloc] initOnCompletion:comBlock onError:errBlock];
    recentNews.purpose = @"recentNews";
    [recentNews startDownloadWithURL:[NSString stringWithFormat:kUrlOfRecentNews,page]];
    [recentNews release];
}

- (void)getLoginUserInfoWithUsername:(NSString *)name
                            password:(NSString *)pwd
                        onCompletion:(CompletionBlock)comBlock 
                             onError:(ErrorBlock)errBlock
{
    BlockDownloader *recentNews = [[BlockDownloader alloc] initOnCompletion:comBlock onError:errBlock];
    recentNews.purpose = @"loginUserInfo";
    NSString* url = [NSString stringWithFormat:kUrlOfGetLoginUserInfo,name,pwd];
    url = [self urlEncodeValue:url];
    [recentNews startDownloadWithURL:url];
    [recentNews release];
}

- (void)getPersonalMessagesWithPage:(NSInteger)page 
                        isReaded:(BOOL)read 
                    onCompletion:(CompletionBlock)comBlock 
                         onError:(ErrorBlock)errBlock
{
    BlockDownloader *personalInfos = [[BlockDownloader alloc] initOnCompletion:comBlock onError:errBlock];
    personalInfos.purpose = @"personalInfos";
    [personalInfos startDownloadWithURL:[NSString stringWithFormat:kUrlOfGetPersonalInfo,[[ZHDataService sharedService] loginUser].userId,page,read]];
    [personalInfos release];
}

- (void)setPersonalMessageIsReadWithMesId:(NSInteger)mesId
                             onCompletion:(CompletionBlock)comBlock 
                                  onError:(ErrorBlock)errBlock
{
    BlockDownloader *messageRead = [[BlockDownloader alloc] initOnCompletion:comBlock onError:errBlock];
    messageRead.purpose = @"messageRead";
    [messageRead startDownloadWithURL:[NSString stringWithFormat:kUrlOfSetMessageRead,[[ZHDataService sharedService] loginUser].userId,mesId]];
    [messageRead release];
}

- (void)getAllProvincesWithLat:(CGFloat)lat 
                         lon:(CGFloat)lon 
                onCompletion:(CompletionBlock)comBlock 
                     onError:(ErrorBlock)errBlock
{
    BlockDownloader *allProvinces = [[BlockDownloader alloc] initOnCompletion:comBlock onError:errBlock];
    allProvinces.purpose = @"allProvinces";
    [allProvinces startDownloadWithURL:[NSString stringWithFormat:kUrlOfGetAllProvincesInfo,lon,lat]];
    [allProvinces release];
}

- (void)getAllDealersWithProvinceId:(NSInteger)proId
                                lat:(CGFloat)lat
                                lon:(CGFloat)lon
                       onCompletion:(CompletionBlock)comBlock 
                            onError:(ErrorBlock)errBlock
{
    BlockDownloader *allDealers = [[BlockDownloader alloc] initOnCompletion:comBlock onError:errBlock];
    allDealers.purpose = @"allDealers";
    [allDealers startDownloadWithURL:[NSString stringWithFormat:kUrlOfGetDealersInfo,lon,lat,proId]];
    [allDealers release];
}

- (void)getForumSectionsOnCompletion:(CompletionBlock)comBlock
                             onError:(ErrorBlock)errBlock
{
    BlockDownloader *forumSections = [[BlockDownloader alloc] initOnCompletion:comBlock onError:errBlock];
    forumSections.purpose = @"forumSections";
    [forumSections startDownloadWithURL:kUrlOfGetSections];
    [forumSections release];
}

- (void)changeUserInfoWithName:(NSString *)name
                        oldPwd:(NSString *)oldPwd
                      province:(NSString *)province
                         email:(NSString *)email
                        newPwd:(NSString *)newPwd
                          city:(NSString *)city
                  onCompletion:(CompletionBlock)comBlock
                           onError:(ErrorBlock)errBlock
{
    BlockDownloader *changeUserInfo = [[BlockDownloader alloc] initOnCompletion:comBlock onError:errBlock];
    changeUserInfo.purpose = @"changeUserInfo";
    NSString *url = [NSString stringWithFormat:kUrlOfChangeUserInfo,name,oldPwd,province,email,newPwd,city];
    url = [self urlEncodeValue:url];
    [changeUserInfo startDownloadWithURL:url];
    [changeUserInfo release];
}

- (void)registerUserWithName:(NSString *)name
                    password:(NSString *)pwd
                       email:(NSString *)email
                    province:(NSString *)province
                        city:(NSString *)city
                onCompletion:(CompletionBlock)comBlock
                     onError:(ErrorBlock)errBlock
{
    BlockDownloader *registerUser = [[BlockDownloader alloc] initOnCompletion:comBlock onError:errBlock];
    registerUser.purpose = @"registerUser";
    NSString *url = [NSString stringWithFormat:kUrlOfRegisterUser,name,pwd,email,province,city];
    url = [self urlEncodeValue:url];
    //NSLog(@"register url:%@",url);
    [registerUser startDownloadWithURL:url];
    [registerUser release];
}

- (void)getThemesWithSectionId:(NSInteger)sid
                          page:(NSInteger)page
                  onCompletion:(CompletionBlock)comBlock
                       onError:(ErrorBlock)errBlock
{
    BlockDownloader *getThemes = [[BlockDownloader alloc] initOnCompletion:comBlock onError:errBlock];
    getThemes.purpose = @"getThemes";
    [getThemes startDownloadWithURL:[NSString stringWithFormat:kUrlOfGetThemes,sid,page]];
    [getThemes release];
}

- (void)getThemeDetailsWithSectionId:(NSInteger)sid
                          page:(NSInteger)page
                  onCompletion:(CompletionBlock)comBlock
                       onError:(ErrorBlock)errBlock
{
    BlockDownloader *getThemeDetails = [[BlockDownloader alloc] initOnCompletion:comBlock onError:errBlock];
    getThemeDetails.purpose = @"getThemeDetailss";
    [getThemeDetails startDownloadWithURL:[NSString stringWithFormat:kUrlOfGetThemeDetails,sid,page]];
    [getThemeDetails release];
}

- (void)sendReply:(NSString*)getParm 
          message:(NSString*)message uploadphoto:(NSData*)uploadphoto 
     onCompletion:(CompletionBlock)comBlock
          onError:(ErrorBlock)errBlock
{
//#define kUrlOfSendReply @"http://www.wdfans.cn/mobile/forum.php?mod=post&action=reply&fid=%d&tid=%d&authcode=%@"
    
    
    
}


@end
